package com.css.fxfzaqbz.modules.assess.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.SecondDisastersEntity;
import com.css.fxfzaqbz.modules.assess.repository.SecondDisastersRepository;
import com.css.fxfzaqbz.modules.assess2.entity.TaskContent;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-09-15 14:14
 */
@Repository
public class SecondDisastersRepositoryImpl implements SecondDisastersRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public SecondDisastersEntity saveSecondDisasters(SecondDisastersEntity secondDisastersEntity) {
        String sql = "insert into sys_second_disasters (ssd_id,ssd_content,ssd_date,ssd_people,ssd_image_id,ssd_image_title,ssd_describe,ssd_mouse_file_id,ssd_area_file_id,ssd_month_title,ssd_area_title,ssd_createtime,ssd_del_flag,ssd_createuser,ssd_relation,types,mark,data_message,area_image,month_image) values(:ssdId,:ssdContent,:ssdDate,:ssdPeople,:ssdImageId,:ssdImageTitle,:ssdDescribe,:ssdMouseFileId,:ssdAreaFileId,:ssdMonthTitle,:ssdAreaTitle,:ssdCreatetime,:ssdDelFlag,:ssdCreateuser,:ssdRelation,:types,:mark,:dataMessage,:areaImage,:monthImage)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(secondDisastersEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return secondDisastersEntity;
    }

    @Override
    public List<SecondDisastersEntity> getAllSecondDisasters(String id) {
        String sql="";
        sql="select u.* from sys_second_disasters u  where u.ssd_del_flag = '"+ YNEnum.N.toString() +"' and u.ssd_relation='"+id+"' and u.mark='"+YNEnum.N.toString()+"'order by u.ssd_createtime desc ";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(SecondDisastersEntity.class));

    }

    @Override
    public SecondDisastersEntity getSecondDisastersById(String id) {
        String sql="";
        sql="select u.* from sys_second_disasters u  where  u.ssd_id='"+id+"' and u.ssd_del_flag = '"+ YNEnum.N.toString() +"'";
        SecondDisastersEntity secondDisastersEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SecondDisastersEntity.class));
        return secondDisastersEntity;
    }

    @Override
    public void deleteSecondDisasters(String id, Timestamp currentTimestamp, String userID) {
        String sql="";
        sql="update sys_second_disasters  set ssd_del_flag ='"+YNEnum.Y.toString()+"',ssd_updateuser='"+userID+"',ssd_updatetime='"+currentTimestamp+"'where ssd_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<SecondDisastersEntity> getSecondDisastersByContent(String ssdContent,String ssdRelation) {
        String sql="";
        sql="select u.* from sys_second_disasters u  where  u.ssd_content='"+ssdContent+"' and u.ssd_relation='"+ssdRelation+"' and u.ssd_del_flag = '"+ YNEnum.N.toString() +"'";

        try {
            List<SecondDisastersEntity> secondDisastersEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SecondDisastersEntity.class));
            return secondDisastersEntities;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public SecondDisastersEntity updateSecondDisastersById(SecondDisastersEntity secondDisastersEntity, String ssdId) {
        String sql = "update  sys_second_disasters  set ssd_content=:ssdContent , ssd_date=:ssdDate,ssd_people=:ssdPeople ,ssd_image_id =:ssdImageId, ssd_image_title=:ssdImageTitle,ssd_describe=:ssdDescribe ,ssd_mouse_file_id=:ssdMouseFileId ,ssd_area_file_id =:ssdAreaFileId,ssd_month_title=:ssdMonthTitle ,ssd_area_title=:ssdAreaTitle,ssd_createtime=:ssdCreatetime, ssd_createuser=:ssdCreateuser,ssd_updateuser=:ssdUpdatetime,ssd_updatetime=:ssdUpdatetime where ssd_id='"+ssdId+"'";

        SqlParameterSource source = new BeanPropertySqlParameterSource(secondDisastersEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return secondDisastersEntity;
    }

    @Override
    public SecondDisastersEntity updateSecondDisasters(SecondDisastersEntity secondDisastersEntity) {
        String sql = "update  sys_second_disasters  set ssd_content=:ssdContent , ssd_date=:ssdDate,ssd_people=:ssdPeople ,ssd_image_id =:ssdImageId, ssd_image_title=:ssdImageTitle,ssd_describe=:ssdDescribe ,ssd_mouse_file_id=:ssdMouseFileId ,ssd_area_file_id =:ssdAreaFileId,ssd_month_title=:ssdMonthTitle ,ssd_area_title=:ssdAreaTitle,ssd_createtime=:ssdCreatetime, ssd_createuser=:ssdCreateuser,ssd_updateuser=:ssdUpdatetime,ssd_updatetime=:ssdUpdatetime,area_image=:areaImage,month_image=:monthImage where ssd_id='"+secondDisastersEntity.getSsdId()+"'";

        SqlParameterSource source = new BeanPropertySqlParameterSource(secondDisastersEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return secondDisastersEntity;
    }

    @Override
    public List<TaskContent> getAllSecondDisasters1(String id) {
        String sql="";
        sql="select ssd_id id,ssd_content as content,ssd_date as dateTime from sys_second_disasters u  where u.ssd_del_flag = '"+ YNEnum.N.toString() +"' and u.ssd_relation='"+id+"'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(TaskContent.class));
    }

    @Override
    public List<SecondDisastersEntity> getAllSecondDisastersTwo(String id) {
        String sql="";
        sql="select u.* from sys_second_disasters u  where u.ssd_del_flag = '"+ YNEnum.N.toString() +"' and u.ssd_relation='"+id+"' and u.mark != '"+YNEnum.N.toString()+"' order by u.ssd_content";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(SecondDisastersEntity.class));

    }

    @Override
    public int getNumber(SecondDisastersEntity secondDisastersEntity) {
        String dzzhtj="地质灾害统计";
        String sql="";
        sql="select u.* from sys_second_disasters u  where  u.ssd_relation='"+secondDisastersEntity.getSsdRelation()+"' and u.ssd_content='"+dzzhtj+"' and u.ssd_del_flag = '"+ YNEnum.N.toString() +"'";

        Integer total = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SecondDisastersEntity.class)).size();

        return total;
    }

    @Override
    public void firstSubmitSecondDisasters(String id, String userID, Timestamp currentTimestamp, String ssdDescribe) {
        String sql="";
        sql="update sys_second_disasters  set mark ='"+YNEnum.Y.toString()+"',ssd_updateuser='"+userID+"',ssd_updatetime='"+currentTimestamp+"',ssd_describe='"+ssdDescribe+"' where ssd_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void firstSubmitSecondDisasters2(String id, String userID, Timestamp currentTimestamp) {
        String sql="";
        sql="update sys_second_disasters  set mark ='"+YNEnum.Y.toString()+"',ssd_updateuser='"+userID+"',ssd_updatetime='"+currentTimestamp+"' where ssd_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public SecondDisastersEntity SecondDisastersIfRepetition(SecondDisastersEntity secondDisastersEntity) {
        String sql="";
        sql="select u.* from sys_second_disasters u  where u.ssd_del_flag = '"+ YNEnum.N.toString() +"' and u.ssd_image_title='"+secondDisastersEntity.getSsdImageTitle()+"' and u.ssd_relation='"+secondDisastersEntity.getSsdRelation()+"' and u.ssd_content='"+secondDisastersEntity.getSsdContent()+"'";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(SecondDisastersEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void addSecondDisastersDisaster(List<String> ids, String describe) {
        String sql = "update sys_second_disasters set ssd_describe = '" + describe + "' " +
                " where ssd_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public void removeSecondDisasters(SecondDisastersEntity secondDisastersById) {
        String sql="";
        sql="update sys_second_disasters  set mark ='"+YNEnum.N.toString()+"',ssd_describe='"+secondDisastersById.getSsdDescribe()+"',submiter='"+secondDisastersById.getSubmiter()+"' ,submit_phone='"+secondDisastersById.getSubmitPhone()+"'   where ssd_id = '"+secondDisastersById.getSsdId()+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void secondSubmitSecondDisasters(List<String> ids, String mobilePhone, String userName,Timestamp currentTimestamp) {
        String sql = "update sys_second_disasters set mark = '" + 2 + "',submiter='"+userName+"',submit_phone='"+mobilePhone+"',ssd_updatetime='"+currentTimestamp+"' ,submit_time='"+currentTimestamp+"'" +
                " where ssd_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public List<SecondDisastersEntity> getSecondDisastersByIaId(String id) {
        String sql = "select * from sys_second_disasters where ssd_relation='" + id + "' and ssd_del_flag='" + YNEnum.N + "' and mark='" + 2 + "'";
        List<SecondDisastersEntity> secondDisastersEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SecondDisastersEntity.class));
        return secondDisastersEntities;
    }

    @Override
    public List<SecondDisastersEntity> getSecondDisastersByIaaId(String iaId) {
        String sql = "select * from sys_second_disasters where ssd_relation='" + iaId + "' and ssd_del_flag='" + YNEnum.N + "' and mark='" + YNEnum.Y + "'";
        List<SecondDisastersEntity> secondDisastersEntities = null;
        try {
            secondDisastersEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SecondDisastersEntity.class));
            return secondDisastersEntities;
        } catch (DataAccessException e) {
           return null;
        }

    }

    @Override
    public SecondDisastersEntity getSecondDisastersByContentAndIaId(String ssdContent, String ssdRelation) {
        String sql="";
        sql="select u.* from sys_second_disasters u  where u.ssd_del_flag = '"+ YNEnum.N.toString() +"' and u.ssd_content='"+ssdContent+"' and u.ssd_relation='"+ssdRelation+"'";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(SecondDisastersEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void deleteTJ(String iaId, Timestamp currentTimestamp, String userIDd) {
        String tj="地质灾害统计";
        String sql="";
        sql="update sys_second_disasters  set ssd_del_flag ='"+YNEnum.Y.toString()+"',ssd_updateuser='"+userIDd+"',ssd_updatetime='"+currentTimestamp+"'  where ssd_relation = '"+iaId+"' and ssd_content='"+tj+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public SecondDisastersEntity check1(SecondDisastersEntity secondDisastersEntity) {
        String sql="";
        sql="select u.* from sys_second_disasters u  where u.ssd_del_flag = '"+ YNEnum.N.toString() +"' and u.ssd_month_title='"+secondDisastersEntity.getSsdMonthTitle()+"' and u.ssd_relation='"+secondDisastersEntity.getSsdRelation()+"'";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(SecondDisastersEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public SecondDisastersEntity check2(SecondDisastersEntity secondDisastersEntity) {
        String sql="";
        sql="select u.* from sys_second_disasters u  where u.ssd_del_flag = '"+ YNEnum.N.toString() +"' and u.ssd_area_title='"+secondDisastersEntity.getSsdAreaTitle()+"' and u.ssd_relation='"+secondDisastersEntity.getSsdRelation()+"'";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(SecondDisastersEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }


}
